package casa.depesos.app.com.http;


import com.google.gson.Gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class BaseParser<T extends BaseBean> {

    Class<T> javaGenericType;

    public BaseParser() {
        Class<?> thisClass = getClass();
        Type genericSuperclass = thisClass.getGenericSuperclass();
        ParameterizedType parameterizedGenericSuperclass = (ParameterizedType) genericSuperclass;
        Type[] typeArgs = parameterizedGenericSuperclass.getActualTypeArguments();
        this.javaGenericType = (Class<T>) typeArgs[0];
    }

    public T parse(String str) throws Exception {
        T value = new Gson().fromJson(str, javaGenericType);
        if (value.isTokenExpired()) {
            //todo
            return value;
        }
        try {
            return parseData(value);
        } catch (Throwable e) {
            e.printStackTrace();
            return value;
        }
    }

    public T parseData(T value) {
        return value;
    }
}
